package com.graduation.smartparkingplatform.Controller;

import com.graduation.smartparkingplatform.Pojo.ClientCharge;
import com.graduation.smartparkingplatform.Pojo.Paid;
import com.graduation.smartparkingplatform.Pojo.SpaceInfo;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/client")
public class ClientController {

  @Autowired ClientService clientService;

  @Value("${Myserver.ip}")
  private String ip;

  @Value("${wxclient.opinionImgPath}")
  private String imgPath;

  //  客户端上传意见
  @PostMapping("/sendOpinion")
  public Response sendOpinion(
      @RequestParam("title") String title,
      @RequestParam("describe") String describe,
      @RequestParam("tel") String tel,
      @RequestParam("file") MultipartFile multipartFile) {
    String fileUrl = null;
    if (multipartFile.isEmpty()) {
      clientService.addOpinion(title, describe, tel, fileUrl);
    }
    String fileName = multipartFile.getOriginalFilename();
    fileName = UUID.randomUUID() + "_" + fileName;
    //    String dirPath = "F:/毕业设计/上传文件测试/";
    File filePath = new File(imgPath);
    if (!filePath.exists()) {
      filePath.mkdirs();
    }
    try {
      multipartFile.transferTo(new File(imgPath + fileName));
      fileUrl = ip + "/image/Opinion/" + fileName; // 需根据实际地址进行修改
      clientService.addOpinion(title, describe, tel, fileUrl);
    } catch (Exception e) {
      return ResponseUtils.buildSYSTEM_ERROR(e);
    }
    return ResponseUtils.buildSuccess("意见提交成功");
  }

  // 客户端获取停车位列表信息
  @GetMapping("/getSpaceInfo")
  public Response getSpaceInfo() {
    try {
      SpaceInfo getSpaceInfo = clientService.getSpaceInfo();
      return ResponseUtils.buildSuccess(getSpaceInfo);
    } catch (Exception e) {
      return ResponseUtils.buildBUSSINESS_ERROR("获取停车位列表信息失败");
    }
  }

  //  客户端绑定车位
  @PostMapping("/chooseSpace")
  public Response chooseSpace(
      @RequestParam("spaceId") Integer spaceId,
      @RequestParam("carPlate") String carPlate,
      @RequestParam("tel") String tel) {
    Boolean chooseSpace = clientService.chooseSpace(spaceId, carPlate, tel);
    if (chooseSpace) {
      return ResponseUtils.buildSuccess("绑定车位成功");
    }
    return ResponseUtils.buildBUSSINESS_ERROR("绑定车位失败");
  }

  //  客户端取消绑定车位
  @PostMapping("/cancelSpace")
  public Response cancelSpace(
      @RequestParam("carPlate") String carPlate, @RequestParam("tel") String tel) {
    String cancelSpace = clientService.cancelSpace(carPlate, tel);
    return ResponseUtils.buildSuccess(cancelSpace);
  }

  // 客户端根据车牌查询停车位（寻车模块）
  @GetMapping("/getSpace")
  public Response getSpace(@RequestParam("carPlate") String carPlate) {
    try {
      Map<String, String> findSpace = clientService.getSpace(carPlate);
      findSpace.put("carPlate", carPlate);
      return ResponseUtils.buildSuccess(findSpace);
    } catch (Exception e) {
      return ResponseUtils.buildBUSSINESS_ERROR("获取车位信息失败");
    }
  }

  //  客户端获取停车费用（付费模块）
  @GetMapping("/price")
  public Response paying(@RequestParam("spaceId") Integer spaceId) throws ParseException {
    try {
      Map<String, Double> paying = clientService.paying(spaceId);
      return ResponseUtils.buildSuccess(paying);
    } catch (Exception e) {
      return ResponseUtils.buildBUSSINESS_ERROR("获取费用失败");
    }
  }

  //  客户端获取收费规则
  @GetMapping("/chargeList")
  public Response getChargeList() {
    try {
      List<ClientCharge> chargeList = clientService.getChargeList();
      return ResponseUtils.buildSuccess(chargeList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  //  客户端获取收费规则
  @GetMapping("/paid")
  public Response getPaidList() {
    try {
      List<Paid> getPaidList = clientService.getPaidList();
      return ResponseUtils.buildSuccess(getPaidList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }
}
